<?php

class Application_Model_Relatorio extends Zend_Db_Table_Abstract {

    protected $_name = 'relatorio';
    protected $_primary = 'codigo';
    private static $instance;

    public static function getInstance() {
        if (!isset(self::$instance)) {
            self::$instance = new Application_Model_Relatorio();
            return self::$instance;
        }
        return self::$instance;
    }

    public function lista($params) {

            $sql = 'select a.dt_atendimento, s.descricao, p.nome, atv.descricao, f.username, a.hora, a.status from atendimento a
                            inner join segmento as s on s.codigo = a.segmento
                            inner join pessoa as p on p.codigo = a.paciente
                            inner join atividade as atv on atv.codigo = a.atividade
                            inner join funcionario as f on f.codigo = a.funcionario';

            if(isset($params["dt_inicio"]) && !empty($params["dt_inicio"]) && isset($params["dt_fim"]) && !empty($params["dt_fim"])){
                $sql.=' and dt_atendimento between \''.Util_Function_Util::formataData($params["dt_inicio"], 'Y/M/d').'\' and \''.Util_Function_Util::formataData($params["dt_fim"], 'Y/M/d').'\'';
            } else if(isset($params["dt_inicio"]) && !empty($params["dt_inicio"]) && isset($params["dt_fim"]) && empty($params["dt_fim"])){
                $sql.=' and dt_atendimento = \''.Util_Function_Util::formataData($params["dt_inicio"], 'Y/M/d').'\'';
            } else if(isset($params["dt_inicio"]) && empty($params["dt_inicio"]) && isset($params["dt_fim"]) && !empty($params["dt_fim"])){
                $sql.=' and dt_atendimento = \''.Util_Function_Util::formataData($params["dt_fim"], 'Y/M/d').'\'';
            }

            if(isset($params["segmentoPaciente"]) && !empty($params["segmentoPaciente"])){
                $sql.=' and segmento = '.$params["segmentoPaciente"].'';
            }

            if(isset($params["atividadePaciente"]) && !empty($params["atividadePaciente"])){
                $sql.=' and atividade = '.$params["atividadePaciente"].'';
            }

            if(isset($params["funcionario"]) && !empty($params["funcionario"])){
                $sql.=' and funcionario = '.$params["funcionario"].'';
            }

            if(isset($params["horarioAtendimento"]) && !empty($params["horarioAtendimento"])){
                $sql.=' and hora = \''.$params["horarioAtendimento"].'\'';
            }

            if(isset($params["status"]) && !empty($params["status"])){
                $sql.=' and status = \''.$params["status"].'\'';
            }

            if (isset($orderName) && isset($order)) {
                $sql .= ' order by ' . $orderName . ' ' . $order;
            }

            $rs = $this->getAdapter()->query($sql)->fetchAll();

            //$rs = $this->getAdapter()->query($sql)->fetchAll();
 

        
        return $rs;
    }

    
}